package org.gl.android.utils.pinyin;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import org.gl.android.utils.pinyin.HanziToPinyin.Token;

public class PinYin {
	// 汉字返回拼音，字母原样返回，都转换为小写
	public static String getPinYin(String input) {
		ArrayList<Token> tokens = HanziToPinyin.getInstance().get(input);
		StringBuilder sb = new StringBuilder();
		if (tokens != null && tokens.size() > 0) {
			for (Token token : tokens) {
				if (Token.PINYIN == token.type) {
					sb.append(token.target);
				} else {
					sb.append(token.source);
				}
			}
		}
		return sb.toString().toLowerCase();
	}

	public static String getFullPinYin(String source) {

		if (!Arrays.asList(Collator.getAvailableLocales()).contains(
				Locale.CHINA)) {

			return source;

		}

		ArrayList<Token> tokens = HanziToPinyin.getInstance().get(source);

		if (tokens == null || tokens.size() == 0) {

			return source;

		}

		StringBuffer result = new StringBuffer();

		for (Token token : tokens) {

			if (token.type == Token.PINYIN) {

				result.append(token.target);

			} else {

				result.append(token.source);

			}
		}
		return result.toString();
	}

	public static String getFirstPinYin(String source) {

		if (!Arrays.asList(Collator.getAvailableLocales()).contains(
				Locale.CHINA)) {

			return source;

		}

		ArrayList<Token> tokens = HanziToPinyin.getInstance().get(source);

		if (tokens == null || tokens.size() == 0) {

			return source;

		}

		StringBuffer result = new StringBuffer();

		for (Token token : tokens) {

			if (token.type == Token.PINYIN) {

				result.append(token.target.charAt(0));

			} else {

				result.append("#");

			}

		}

		return result.toString();

	}
}
